#!/usr/bin/python
# -*- coding:utf-8 -*- 
import urllib2
import httplib
import re
from sgmllib import SGMLParser
 
class ListName(SGMLParser):
	def __init__(self):
		SGMLParser.__init__(self)
		self.is_a = ""
		self.tmp=""
		self.name = []
	def start_a(self, attrs):
	    if len(attrs)==2:
	        if attrs[0][0]=="href" and attrs[1][0]=="target" and attrs[1][1]=="_blank":
	            self.tmp=attrs[0][1]+";"
	            self.is_a=1                
	def end_a(self):
		self.is_a = ""
	def handle_data(self, text):
		if self.is_a == 1:
		    self.name.append(self.tmp+text)
listname = ListName()
queryString="构建新的图书馆服务"
#对于某些搜索，可能不准确！！！！需要修改
url="http://s.g.wanfangdata.com.cn/Paper.aspx?q="

opener = urllib2.build_opener()
opener.addheaders = [("Host","s.g.wanfangdata.com.cn"),("Connection","keep-alive"),\
                    ("User-Agent","Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11"),\
                    ("Accept","text/plain"),\
                    ("Accept-Language","zh-CN,zh;q=0.8"),\
                    ("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3")]
page=opener.open(url+urllib2.quote(queryString))

response=page.read()
serverCookie=page.info()["Set-Cookie"]
print serverCookie
#f=open("/home/g5tf87/tes.html","r")
#response=f.read()
#f.close()'''
rsstring=""
listname.feed(response)
for item in listname.name:
	tmp=item.split(";")
	#print tmp[1].decode("utf-8")
	if tmp[1].decode("utf-8")==queryString.decode("utf-8"):
	   rsstring=tmp[0].split("/")[3].split(".")[0]
rs="rs=|"+rsstring+"|"
#此处是单个rs，多个时还需要修改
sendCookie="Hm_lvt_f5e6bd27352a71a202024e821056162b=1351412659925; Hm_lpvt_f5e6bd27352a71a202024e821056162b=1351412659925; WFKS.Auth=%7b%22AuthenticationContext%22%3a%7b%22AccountIds%22%3a%5b%7b%22AccountType%22%3a%22Group%22%2c%22Key%22%3a%22huabeidianli%22%7d%2c%7b%22AccountType%22%3a%22GTimeLimit%22%2c%22Key%22%3a%22huabeidianli%22%7d%5d%2c%22AuthenticationSign%22%3a%22fy2QZg%2bk975DvBsHHd514br7mDCjL4q7tPZhCSQN2X1zRNSg5it4X16LnwIdLICQ%22%2c%22Data%22%3a%5b%7b%22Key%22%3a%22Group.huabeidianli.DisplayName%22%2c%22Value%22%3a%22%e5%8d%8e%e5%8c%97%e7%94%b5%e5%8a%9b%e5%a4%a7%e5%ad%a6%22%7d%5d%2c%22SessionId%22%3a%22e4ea80b1-af15-4c40-86c5-e73f99d01415%22%7d%2c%22LastUpdate%22%3a%22%5c%2fDate(1351412677000%2b0800)%5c%2f%22%2c%22Sign%22%3a%22H6UjgnBDDH4t+k\/96T8dhA==%3d%3d%22%7d; rs=%7C"+rsstring+"%7C"

url = 'http://s.g.wanfangdata.com.cn/Export/Export.aspx?'

opener = urllib2.build_opener()
opener.addheaders = [("Cookie",sendCookie)]
retPage=opener.open(url)
print retPage.read()
